#ifndef ROADMAPITEM_H
#define ROADMAPITEM_H

#include <QGraphicsItemGroup>

#include "../opxml/rwxml.h"
#include "lane.h"
#include "mapinfo.h"
#include "roaditem.h"
#include "commondefine.h"

#define S_COORDINATE "s"
#define X_COORDINATE "x"
#define Y_COORDINATE "y"
#define JUNCTION_STR "junction"
#define HDG_STR "hdg"
#define LENGTH_STR "length"
#define LINK_STR "link"
#define SUCCESSOR_STR "successor"
#define ELEMENTTYPE "elementType"
#define ELEMENTID "elementId"
#define CONTACTPOINT "contactPoint"
#define ID_STR "id"
#define CONNECTION "connection"
#define INCOMINGROAD "incomingRoad"
#define CONNECTINGROAD "connectingRoad"
#define GEOMETRY_STR "geometry"
#define ELEVATIONPROFILE_STR "elevationProfile"
#define ELEVATION_STR "elevation"
#define START "start"
#define LINE_STR "line"
#define ARC_STR "arc"
#define SPIRAL_STR "spiral"
#define POLY3_STR "poly3"
#define CURVATURE_STR "curvature"
#define CURSTART_STR "curvStart"
#define CUREND_STR "curvEnd"
#define ROAD "road"
#define PLANVIEW_STR "planView"
#define LANES_STR "lanes"
#define LANEOFFSET_STR "laneOffset"
#define LANESECTION_STR "laneSection"
#define LANE_STR "lane"
#define LANE_ITEM_TYPE "type"
#define WIDTH_STR "width"
#define ROADMARK_STR "roadMark"
#define COLOR_STR "color"
#define WIDTH_PARAMETER_A "a"
#define WIDTH_PARAMETER_B "b"
#define WIDTH_PARAMETER_C "c"
#define WIDTH_PARAMETER_D "d"
#define NONE_STR "none"
#define STANDARD "standard"

typedef QVector<QPointF> LanePaint;
typedef QVector<LanePaint> LaneSectionPaint;
typedef QVector<LaneSectionPaint> RoadPaint;
typedef QVector<RoadPaint> RoadMapPaint;
typedef QVector<SLane> SectionLane;
typedef QVector<SectionLane> RoadLane;
typedef QVector<RoadLane> RoadMapLane;

struct Painting
{
    QPolygonF polygonF;
    QString qstrColor;
    QString qstrStyle;
};

class RoadMapItem: public QObject, public QGraphicsItemGroup
{
    //如果需要信号和槽，必须继承QObject
    Q_OBJECT

public:
    RoadMapItem();
    RoadMapItem(const char* path);

    void paint(QPainter *painter,const QStyleOptionGraphicsItem*,QWidget*) override;

    RwXml *getDom();

    QVector<Junction> getJunction() const;
    void setJunction(const QVector<Junction> &qvecJunction);
    QVector<SController> getController() const;
    void setController(const QVector<SController> &qvecController);
    void setRoadMapPaint(const RoadMapPaint &roadMapPaint);
    void setRoadMapLane(const RoadMapLane &roadMapLane);
    void setFilling(const QVector<Painting> &qvecFilling);
    void setLaneLine(const QVector<Painting> &qvecLaneLine);
    void appendFilling(const Painting filling);
    void appendLaneLine(const Painting laneLine);

private:

    // 获取解析的全部道路信息
    int getXmlParseData(QVector<Road>& AllRoad);

    // 根据解析获得的道路信息，获取道路表面轮廓点坐标
    int getRoadSurfaceContourPoint(QVector<QVector<QVector<QVector<QPointF>>>> &AllPoint ,const QVector<Road> AllRoad);

    void getRoadInformation();

    // 获取车道段数据
    QVector<QVector<QPointF>> getLaneSectionPoint(LaneOffset &laneOffset, LaneSection &laneSection, QVector<Geometry> &vecGeometry, double dlaneStart, double dlaneEnd);

    // 将相对车道段合并
    void mergeAllRelativelyLaneSection(QVector<QVector<QPointF>> &vecEachLaneSection, const QVector<QVector<QVector<QPointF>>> vecAllRelativelyLaneSection);

    // 获取地理几何元素数据
    QVector<QVector<QPointF>> getGeometryData(LaneOffset &laneOffset, LaneSection &laneSection, Geometry &geometry, double dstartds, double dendds);

    // 获取车道数据
    void getGeometryLaneData(Geometry &geometry, LaneOffset &laneOffset, Lane &lane, double sOffset, double dstartds, double dendds, QVector<QVector<QPointF>> &lanesPoint);

private:
    RwXml *m_pRwXml;
    QVector<Junction> m_qvecJunction;
    QVector<SController> m_qvecController;
    QVector<Painting> m_qvecFiling;  //填充
    QVector<Painting> m_qvecLaneLine;  //车道线
    RoadMapPaint m_roadMapPaint;
    RoadMapLane m_roadMapLane;

public slots:
    void onMap(RoadItem *pRoadItem, LaneItem *pLaneItem, const QPointF &pointf);

signals:
    void sigSendMapInfo(RoadItem *pRoadItem, LaneItem *pLaneItem, const QPointF &pointf);
};

#endif // ROADMAPITEM_H
